package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.cross.CrossGeneralActivityUnionRankModel;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.CrossGeneralActivityRankMgr;
import com.yanqu.road.server.manager.peakstronghold.CrossPeakStrongholdMgr;
import com.yanqu.road.server.manager.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manager.peakstronghold.CrossPeakStrongholdMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.C_CROSS_GENERAL_UNION_RANK, desc = "")
public class CrossGeneralActivityUnionRankCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long userId = packet.getPlayerId();
        StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg req = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        int conditionType = req.getConditionType();
        String unionUid = req.getUnionUid();

        int unionRank = CrossGeneralActivityRankMgr.getCrossActivityUnionRank(activityId, conditionType, serverId, unionUid);
        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        if (conditionType == eGamePlayerEventType.PeakSeacraftUnionRank.getValue()) {
            PeakSeacraftMgr.parseUnionRankMsgByUserId(activityId, userId, msg);
        } else {
            msg.setMyRank(unionRank);
            CrossGeneralActivityUnionRankModel model = CrossGeneralActivityRankMgr.getCrossActivityUnionRankListModel(activityId, conditionType);
            BigInteger myUnionValue = model.getMyUnionValue(serverId, unionUid);
            msg.setValue(myUnionValue.toString());
            UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = model.getRankListMsg();
            msg.setRankList(rankListMsg);
        }

        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK_LIST, msg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }


    @Override
    public void sendData() {

    }
}
